package com.apes.enquiry.supplier.baturu.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.frame.model.Store;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = BaturuInquire.CONST_TABLE_NAME)
public class BaturuInquire extends Model {
    public static final String CONST_TABLE_NAME = "sp_buturu_inquire";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "巴图鲁-询价", prefix = "BI%(yy)%(month)", padding = 6, useDateRange=true)
    @Comment("编号")
    private String id;

    @Comment("上游单号")
    @Column(name = "origin")
    private String origin;

    @Comment("上游单据类型")
    @Column(name = "origin_business_type")
    private String originBusinessType;

    @Comment("巴图鲁询价单号")
    @Column(name = "baturu_inquire_id")
    private String baturuInquire;

    @Comment("门店")
    @Column(name = "store_id")
    private String store;

    @Comment("询价联系人")
    @Column(name = "contact_person_id")
    private String contactPerson;

    @Comment("询价联系人电话")
    @Column(name = "contact_phone")
    private String contactPhone;

    @Comment("发票类型")
    @Column(name = "invoice_type", columnDefinition = "number(1,0) default -1")
    private int invoiceType = -1;

    @Comment("报价状态") // 1：报价中；2：已报价；3：已打回；4：已失效。
    @Column(name = "quote_status", columnDefinition = "number(1,0) default 1")
    private int quoteStatus = 1;

    @Comment("过期时间")
    @Column(name = "expired_time")
    private Date expiredTime;

    @Comment("客服打回原因")
    @Column(name = "beat_back_reason")
    private String beatBackReason;

    @Comment("备注")
    @Column(name = "remark")
    private String remark;

//    @Comment("配件图片 URL 集合")
//    @Column(name = "part_pictures")
//    private String partPictures; // 最多 10 张，配件图片无法关联到具体配件时使用

    @OneToMany(mappedBy = "baturuInquire", orphanRemoval = true, cascade = CascadeType.ALL)
    private Set<BaturuInquireCarInfo> carInfos = new HashSet<>();
    public Set<BaturuInquireCarInfo> getCarInfos() {
        for (BaturuInquireCarInfo carInfo:carInfos) {
            carInfo.setBaturuInquire(this);
        }
        return carInfos;
    }

    @OneToMany(mappedBy = "baturuInquire", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<BaturuInquireItem> inquiryDetails = new HashSet<>();
    public Set<BaturuInquireItem> getInquiryDetails() {
        for (BaturuInquireItem item:inquiryDetails) {
            item.setBaturuInquire(this);
        }
        return inquiryDetails;
    }
}
